stage3-ag6 << 
Previous Next >> stage3-2a
task 1
讀取 stage3_2a.txt, 建立 Stage3 的分組倉儲, 分組網頁, 以及各組員倉儲及網頁連結.
老師的範例:
# open file, default is read mode, since txt content no chinese char
# no encoding = "UTF-8" is needed
with open("stage3_2a.txt") as fh:
    # readlines will read into the whole line and put into list format
    # has \n at the end of each line
    data = fh.readlines()
#print(len(data))
for i in range(len(data)):
    group = data[i].rstrip("\n").split("\t")
    print(group)
# the following will use group data to generate needed html
第一版:將學號按組別排好
with open("stage3_2a.txt") as fh:
   #先將我們存起來stage3_2a.txt打開並命名為fh
    data = fh.readlines()
    #將stage3_2a.txt的資料以串列形式存為data
for i in range(len(data)):
    group = data[i].rstrip("\n").split("\t")
    #先取出data list中的第i項,消除元素中/n,再以\t取出需要的文字
    print(group[0]+'|'+group[0])
    #先將group第0列推出
    for j in range(1,18,1):
    #設一個範圍,(1到18,每次加1,1<=j<18)
        try:
            print(group[j])
        except:
           continue
        #這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑
第二版:將40823122的學號修正
with open("stage3_2a.txt") as fh:
   #先將我們存起來stage3_2a.txt打開並命名為fh
    data = fh.readlines()
    #將stage3_2a.txt的資料以串列形式存為data
for i in range(len(data)):
    newdata = data[i].replace('4823122','40823122')
    #因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
    group = newdata.rstrip("\n").split("\t")
    #先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
    print(group[0]+'|'+group[0])
    #先將group第0列推出
    for j in range(1,18,1):
    #設一個範圍,(1到18,每次加1,1<=j<18)
        try:
            print(group[j])
        except:
           continue
        #這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑
第三版:以網頁的形式推出,但會告知
print('<p>'+group[0]+' | <a href="https://'+group[1]+'.github.io/'+group[0]+'">Website</a> | <a href="https://github.com/'+group[2]+'/'+group[0]+'">Repository</a></p>')
IndexError: list index out of range
with open("stage3_2a.txt") as fh:
   #先將我們存起來stage3_2a.txt打開並命名為fh
    data = fh.readlines()
    #將stage3_2a.txt的資料以串列形式存為data
for i in range(len(data)):
    newdata = data[i].replace('4823122','40823122')
    #因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
    group = newdata.rstrip("\n").split("\t")
    #先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
    print('<p>'+group[0]+' | <a href="https://'+group[1]+'.github.io/'+group[0]+'">Website</a> | <a href="https://github.com/'+group[2]+'/'+group[0]+'">Repository</a></p>')
    for j in range(1,18,2):
    #設一個範圍,(1到18,每次+2,1<=j<18)
        try:
            print('<p>'+group[j]+' | Website:'+'<a href="https://'+group[j]+'.github.io/cd2021'+'">'+group[j]+'</a>'+' | Repository:'+'<a href="https://github.com/'+group[j]+'/cd2021'+'">'+group[j]+'</a> </p>')
        except:
           continue
        #這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑
第四版:將a40823112和stage3_ag修正
with open("stage3_2a.txt") as fh:
   #先將我們存起來stage3_2a.txt打開並命名為fh
    data = fh.readlines()
    #將stage3_2a.txt的資料以串列形式存為data
for i in range(6):
    #將次數限定就不會告知list index out of range了
    newdata1 = data[i].replace('4823122','40823122')
    #因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
    newdata2 = newdata1.replace('\t\t','')
    #因為有一組只有6人,所以用 replace 把空位刪除
    newdata3 = newdata2.replace('_','-')
    #因為在編輯txt的時候-會變成_,所以用 replace 把_修正為-
    group = newdata3.rstrip("\n").split("\t")
    #先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
    print('<p>'+group[0]+' | <a href="https://'+group[1]+'.github.io/'+group[0]+'">Website</a> | <a href="https://github.com/'+group[2]+'/'+group[0]+'">Repository</a></p>')
    for j in range(1,18,2):
    #設一個範圍,(1到18,每次+2,1<=j<18)
        try:
            n = group[j].replace('40823112','a40823112')
            #因為40823112的github帳號是a40823112,所以用 replace 把帳號修正
            print('<p>'+group[j]+' | Website:'+'<a href="https://'+n+'.github.io/cd2021'+'">'+group[j]+'</a>'+' | Repository:'+'<a href="https://github.com/'+n+'/cd2021'+'">'+group[j]+'</a> </p>')
        except:
           continue
        #這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑
第五版:將連結以老師常用的形式推出
with open("stage3_2a.txt") as fh:
   #先將我們存起來stage3_2a.txt打開並命名為fh
    data = fh.readlines()
    #將stage3_2a.txt的資料以串列形式存為data
for i in range(6):
    #將次數限定就不會告知list index out of range了
    newdata1 = data[i].replace('4823122','40823122')
    #因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
    newdata2 = newdata1.replace('\t\t','')
    #因為有一組只有6人,所以用 replace 把空位刪除
    newdata3 = newdata2.replace('_','-')
    #因為在編輯txt的時候-會變成_,所以用 replace 把_修正為-
    group = newdata3.rstrip("\n").split("\t")
    #先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
    print('<p><a href="https://github.com/'+group[1]+'/'+group[0]+'">'+group[0]+' repo</a> | <a href="https://'+group[2]+'.github.io/'+group[0]+'">'+group[0]+' site</a></p>')
    for j in range(1,18,2):
    #設一個範圍,(1到18,每次+2,1<=j<18)
        try:
            n = group[j].replace('40823112','a40823112')
            #因為40823112的github帳號是a40823112,所以用 replace 把帳號修正
            print('<p>'+'<a href="https://github.com/'+n+'/cd2021">'+group[j]+' repo</a> | <a href="https://'+n+'.github.io/cd2021">'+group[j]+' site</a></p>')
        except:
           continue
        #這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑
Anti-piracy system:
with open("stage3_2a.txt") as fh:
   #先將我們存起來stage3_2a.txt打開並命名為fh
    data = fh.readlines()
    #將stage3_2a.txt的資料以串列形式存為data
for i in range(6):
    #將次數限定就不會告知list index out of range了
    newdata1 = data[i].replace('4823122','40823122')
    #因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
    newdata2 = newdata1.replace('\t\t','')
    #因為有一組只有6人,所以用 replace 把空位刪除
    group = newdata3.rstrip("\n").split("\t")
    #先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
    print('<p><a href="https://github.com/'+group[1]+'/'+group[0]+'">'+group[0]+' repo</a> | <a href="https://'+group[2]+'.github.io/'+group[0]+'">'+group[0]+' site</a></p>')
    for j in range(1,18,2):
    #設一個範圍,(1到18,每次+2,1<=j<18)
        try:
            n = group[j].replace('40823112','a40823112')
            n = group[j].replace('4082312','40823125')
            print('<p>'+'<a href="https://github.com/'+n+'/cd2021">'+group[j]+' repo</a> | <a href="https://'+n+'.github.io/cd2021">'+group[j]+' site</a></p>')
        except:
           continue
        #這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑
參考資料:
python 語法 renge
python 基本語法
python loop 原理
40823136 Website
stage3-ag6 << 
Previous Next >> stage3-2a